home *** CD-ROM | disk | FTP | other *** search
- #include <math.h>
- #include "ModuleRoutines.h"
- #include "Jules.h"
-
-
- /*--------------------------------------------
- SmoothWalk()
- ---------------------------------------------*/
-
- void SmoothWalk(WindowPtr wind)
- {
- JulesData juleData;
-
- InitJules(&juleData, &wind->portRect);
-
- while(!Button())
- {
- GetNextPoint(&juleData);
- JuliaStep(&juleData, 10, false); // Throw away first few points
- JuliaStep(&juleData, 30, true); // Draw a bunch
- if(Random() % 20 == 0)
- {
- JuliaStep(&juleData, kNumJuliaPts, true); // Once in a while, do 'em all
- }
- }
- InvalRect(&wind->portRect);
- }
-
- /*--------------------------------------------
- InitJules() Sets up from the given window dimensions
- ---------------------------------------------*/
-
- void InitJules(JulesDataPtr julePtr, Rect *monR)
- {
- short xdim, ydim;
- double_t pixelWidth, pixelHeight, xOffset, yOffset;
- //RGBColor draw = {64000, 0, 0};
- RGBColor draw;
-
- // set number of Jules pts
- julePtr->numJuliaPts = kNumJuliaPts;
-
- // Set up scaling constant
- #if defined(powerc) || defined (__powerc)
- julePtr->randDivider = 32767.0 * 1000.0;
- #else
- julePtr->randDivider = 32767.0 * 100.0 * 3; // added a few pts 'cause 881 is fast
- #endif
-
- // Get size of window
- xdim = monR->right - monR->left;
- ydim = monR->bottom - monR->top;
-
- // Calculate the # of pixels per unit distance for the julePtrs
- julePtr->xPixPerUnit = (double_t)xdim / 3.0;
- julePtr->yPixPerUnit = (double_t)ydim / 3.0;
-
- // •• Set up stuff for random walk ••
-
- // initial velocity
- julePtr->deltaX = (double_t)Random() / julePtr->randDivider;
- julePtr->deltaY = (double_t)Random() / julePtr->randDivider;
-
- // Find box in pixel space
- julePtr->boundsLeft = (double_t)xdim / 6;
- julePtr->boundsRight = (double_t)xdim - julePtr->boundsLeft;
- julePtr->boundsTop = (double_t)ydim / 6;
- julePtr->boundsBot = (double_t)ydim - julePtr->boundsTop;
-
- // Convert to Mandel Space
- pixelWidth = 3.0 / (double_t)xdim;
- pixelHeight = 2.4 / (double_t)xdim;
- xOffset = 2.0;
- yOffset = 1.2;
-
- julePtr->boundsLeft = -xOffset + (julePtr->boundsLeft * pixelWidth);
- julePtr->boundsTop = -yOffset + (julePtr->boundsTop * pixelHeight);
- julePtr->boundsRight = -xOffset + (julePtr->boundsRight * pixelWidth);
- julePtr->boundsBot = -yOffset + (julePtr->boundsBot * pixelHeight);
-
- // Current point, start in center of bounds
- julePtr->currentX = julePtr->boundsLeft + ((julePtr->boundsRight - julePtr->boundsLeft) / 2);
- julePtr->currentY = julePtr->boundsTop + ((julePtr->boundsBot - julePtr->boundsTop) / 2);
-
- // •• Set up color ramp stuff ••
- julePtr->drawColor = draw;
-
- // Set up color ramping increments
- julePtr->curHue = 0;
- //julePtr->rInc = Random() % 256;
- //julePtr->gInc = Random() % 256;
- //julePtr->bInc = Random() % 256;
-
- // Calculate half the window dimensions
- julePtr->halfXDim = xdim / 2 + monR->left;
- julePtr->halfYDim = ydim / 2 + monR->top;
-
- // set up initial julePtr points outside window
- julePtr->ptIndex = 0;
- {
- short cnt;
-
- for(cnt = 0; cnt < kNumJuliaPts; cnt++)
- julePtr->juliaPts[cnt].h = julePtr->juliaPts[cnt].v = -1;
- }
- }
-
-
- void JuliaStep(JulesDataPtr julePtr, short numPts, Boolean drawIt)
- {
- long count;
- short m, n;
- double_t wx, wy, theta, r, x, y;
- const double_t pi = 3.14159, piOver2 = 1.570795;
-
- // Ramp the color
- if ( julePtr->useColor && TickCount() %4 == 0) {
- julePtr->curHue++;
- if ( julePtr->curHue > 360)
- julePtr->curHue = 0;
- CalcHSBtoRGB( julePtr->curHue, 100, 100, &julePtr->drawColor);
- }
- //julePtr->drawColor.red += julePtr->rInc;
- //julePtr->drawColor.green += julePtr->gInc;
- //julePtr->drawColor.blue += julePtr->bInc;
-
- x = y = 0.1;
- for(count = 0; count < numPts; count++)
- {
- wx = x - julePtr->currentX;
- wy = y - julePtr->currentY;
-
- if(wx > 0.0)
- theta = atan(wy/wx);
- else if(wx < 0.0)
- theta = pi + atan(wy/wx);
- else
- theta = piOver2;
-
- theta /= 2;
-
- r = sqrt(wx * wx + wy * wy);
- if(Random() < 0)
- r = sqrt(r);
- else
- r = -sqrt(r);
-
- x = r * cos(theta);
- y = r * sin(theta);
-
- if(drawIt)
- {
- PenMode(srcCopy);
-
- // Erase old
- MoveTo(julePtr->juliaPts[julePtr->ptIndex].h, julePtr->juliaPts[julePtr->ptIndex].v);
- //RGBForeColor(&julePtr->eraseColor);
- ForeColor(blackColor);
- Line(0, 0);
-
- // Convert to window coords
- julePtr->juliaPts[julePtr->ptIndex].h = m = (short)(x * julePtr->xPixPerUnit) + julePtr->halfXDim;
- julePtr->juliaPts[julePtr->ptIndex].v = n = (short)(y * julePtr->yPixPerUnit) + julePtr->halfYDim;
- julePtr->ptIndex++;
-
- // Draw new
- MoveTo(m, n);
- if ( julePtr->useColor) RGBForeColor(&julePtr->drawColor);
- else ForeColor( whiteColor);
- Line(0, 0);
-
- // Keep julePtr->ptIndex in range
- if(julePtr->ptIndex >= kNumJuliaPts)
- julePtr->ptIndex %= kNumJuliaPts;
- }
- }
- }
-
- void GetNextPoint(JulesDataPtr julePtr)
- {
- double_t localH, localV;
- double_t maxSpeed, minSpeed;
-
- localH = julePtr->currentX;
- localV = julePtr->currentY;
-
- localH += julePtr->deltaX;
- localV += julePtr->deltaY;
-
- #if defined(powerc) || defined (__powerc)
- maxSpeed = 0.004;
- minSpeed = 0.0001;
- #else
- maxSpeed = 0.04;
- minSpeed = 0.001;
- #endif
-
- if(localH > julePtr->boundsRight)
- {
- localH = julePtr->boundsRight;
- julePtr->deltaX = -julePtr->deltaX;
- julePtr->deltaX += (double_t)Random() / julePtr->randDivider;
- julePtr->deltaY += (double_t)Random() / julePtr->randDivider;
- }
- if(localH < julePtr->boundsLeft)
- {
- localH = julePtr->boundsLeft;
- julePtr->deltaX = -julePtr->deltaX;
- julePtr->deltaX += (double_t)Random() / julePtr->randDivider;
- julePtr->deltaY += (double_t)Random() / julePtr->randDivider;
- }
- if(localV > julePtr->boundsBot)
- {
- localV = julePtr->boundsBot;
- julePtr->deltaY = -julePtr->deltaY;
- julePtr->deltaX += (double_t)Random() / julePtr->randDivider;
- julePtr->deltaY += (double_t)Random() / julePtr->randDivider;
- }
- if(localV < julePtr->boundsTop)
- {
- localV = julePtr->boundsTop;
- julePtr->deltaY = -julePtr->deltaY;
- julePtr->deltaX += (double_t)Random() / julePtr->randDivider;
- julePtr->deltaY += (double_t)Random() / julePtr->randDivider;
- }
-
- // adjust speed
- if(julePtr->deltaX > maxSpeed) julePtr->deltaX = maxSpeed;
- else if(julePtr->deltaX < -maxSpeed) julePtr->deltaX = -maxSpeed;
- else if(julePtr->deltaX == 0.0) julePtr->deltaX = 0.001;
- if(julePtr->deltaY > maxSpeed) julePtr->deltaY = maxSpeed;
- else if(julePtr->deltaY < -maxSpeed) julePtr->deltaY = -maxSpeed;
- else if(julePtr->deltaY == 0.0) julePtr->deltaY = 0.001;
-
- // save new value in struct
- julePtr->currentX = localH;
- julePtr->currentY = localV;
- }
-